// pages/topicality/xiaofeiquan/xiaofeiquan.js
//获取应用实例
const app = getApp()
const util = require("../../../utils/util.js");
var QQMapWX = require('../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
    key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
Page({

    /**
     * 页面的初始数据
     */
    data: {
        citycode: "bj",
        city: '北京',
        province: '北京',
        pindex: 0,
        cindex: 0,
        qindex: 0,
        // cityPick: ["选择城市"],
        quPick: ["全市"],
        showtk: false,
        showNav: true, // 快捷导航是否展开
        showShare: true, // 显示分享按钮

        showTitle1: true, // 患者到访小区分布图 显示
        showTitle2: false, // 确诊病例区域分布图 显示

        longitude: 116.38926392898001, //默认定位经度
        latitude: 39.923697898213106, //默认定位纬度
        markers: [],

        realTimeData: [], // 实时新增数据
        showReal: false, // 查看全部实时新增

        guijiData: [], // 病例轨迹数据
        showGuiji: false, // 查看全部病例轨迹数据

        resMarkers: [],
        showNomal: true,

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        util.getUrl(app.globalData.url).then((res) => {
            this.getProvinceData(options.city);

        })
    },

    // 定位
    getLocation() {
        qqmapsdk.reverseGeocoder({
            success: (res) => {
                let city = res.result.address_component.city;
                // let city="芜湖市";
                let citydata = this.data.citylist.filter(v => {
                    if (city.indexOf(v.cityname) != -1) {
                        return v;
                    }
                });

                if (citydata != '' && city.indexOf(this.data.city) == -1) {
                    wx.showModal({
                        content: '您当前定位城市为' + city + '\n是否需要切换到' + city + '新冠肺炎患者分布小区查询？',
                        success: res => {
                            if (res.confirm) {
                                this.setData({
                                    city: Array.isArray(citydata) ? citydata[0].cityname : this.data.city,
                                    citycode: Array.isArray(citydata) ? citydata[0].citycode : this.data.citycode,
                                    province: Array.isArray(citydata) ? citydata[0].province : this.data.province,
                                    // cityPick: ["选择城市"]
                                    cindex: 0,
                                })
                                let clist = [];
                                this.data.provPick.forEach((v, k) => {
                                    if (v == this.data.province) {
                                        this.setData({
                                            pindex: k,
                                        })
                                    }
                                })
                                this.data.cityArray[this.data.pindex].citylist.forEach(v => {
                                    clist.push(v.cityname)
                                })
                                this.setData({
                                    // cityPick: this.data.cityPick.concat(clist)
                                    cityPick: clist
                                })
                                this.data.cityPick.forEach((v, k) => {
                                        if (v == this.data.city) {
                                            this.setData({
                                                cindex: k,
                                            })
                                        }
                                    })
                                    // 获取城市公众号二维码
                                this.getQrcode(this.data.citycode);
                                this.getInfoData(this.data.citycode);
                            }
                        }
                    })
                }

            },
        });
    },

    // 获取城市公众号二维码
    getQrcode(citycode) {
        util.cityConfig(citycode).then(res => {
            this.setData({
                qrcodeimg: res.qrcode,
            })
        })
    },

    // 获取有疫情等级的城市
    getProvinceData(optcode) {
        let data = {
            action: 'xiaoqu',
            type: "city",
        };
        util.request('zhuanti.php', data, ).then(res => {
            this.setData({
                cityArray: res.data.data.city,
            })
            let provPick = [];
            let citylist = [];
            let clist = [];
            this.data.cityArray.forEach(v => {
                v.citylist.forEach(v => {
                    citylist.push(v)
                })
                provPick.push(v.province)
                this.setData({
                    provPick,
                    citylist
                })
            })
            if (this.data.citylist) {
                this.data.citylist.forEach((v, k) => {
                    if (optcode == v.citycode) {
                        this.setData({
                            citycode: v.citycode,
                            city: v.cityname,
                            province: v.province,
                        })
                    } else {
                        this.setData({
                            citycode: this.data.citycode,
                            city: this.data.city,
                            province: this.data.province,
                        })
                    }
                })
            }

            this.data.provPick.forEach((v, k) => {
                if (v == this.data.province) {
                    this.setData({
                        pindex: k,
                    })
                }
            })

            this.data.cityArray[this.data.pindex].citylist.forEach(v => {
                clist.push(v.cityname)
            })
            this.setData({
                // cityPick: this.data.cityPick.concat(clist)
                cityPick: clist
            })

            this.data.cityPick.forEach((v, k) => {
                    if (v == this.data.city) {
                        this.setData({
                            cindex: k,
                        })
                    }
                })
                // 获取城市公众号二维码
            this.getQrcode(this.data.citycode);
            this.getInfoData();

            if (this.data.citylist != "" && Array.isArray(this.data.citylist)) {
                // 获取定位
                this.getLocation();
            }
        })
    },

    // 获取数据
    getInfoData(citycode) {
        let data = {
            action: 'xiaoqu',
            citycode: citycode ? citycode : this.data.citycode
        };
        util.request('zhuanti.php', data, ).then(res => {
            if (res.data.data.daofen) {
                res.data.data.daofen.content = res.data.data.daofen.content
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(="[^"]+")))/ig, '<p')
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p')
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(class="[^"]+")))/ig, '<p')
                    .replace(/<p>/ig, '<p class="p_class">')
                    .replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1')
                    .replace(/<img/gi, '<img style="width:100%;height:auto"')
                    // .replace(/<span>/ig, '<p class="p_class">')
            }
            wx.setNavigationBarTitle({
                    title: res.data.data ? res.data.data.title : "新冠肺炎患者分布小区查询",
                })
                // 处理区域选择
            let quData = res.data.data.qu;
            if (quData) {
                let qlist = [];
                quData.forEach(v => {
                    qlist.push(v.cityname)
                })
                this.setData({
                    quPick: this.data.quPick.concat(qlist),
                    quData,
                })
            }

            // 处理城市经纬度
            if (res.data.data.jwd != '') {
                let city_jd = res.data.data.jwd.city_jd;
                let city_wd = res.data.data.jwd.city_wd;
                let jwdData = util.bMapTransQQMap(city_jd, city_wd);
                this.setData({
                    longitude: jwdData.lng,
                    latitude: jwdData.lat,
                })
            }

            // 处理地图上标记点
            let marketData = res.data.data.xqname;
            let marketInfo = res.data.data.xqname.splice(0, 40);
            if (marketInfo) {
                marketInfo.forEach((v, k) => {
                    if (v.location) {
                        v.location.lng = util.bMapTransQQMap(v.location.lng, v.location.lat).lng;
                        v.location.lat = util.bMapTransQQMap(v.location.lng, v.location.lat).lat;
                        this.data.markers.push({
                            id: k,
                            longitude: v.location.lng,
                            latitude: v.location.lat,
                            iconPath: '/images/dwl.png',
                            label: {
                                content: v.address,
                                bgColor: "#fff",
                                fontSize: '20rpx',
                                padding: "10rpx",
                                borderColor: "#232E7D",
                                borderWidth: "1rpx",
                                borderRadius: "10rpx",
                                textAlign: 'center',
                                color: "#ff0000"
                            }
                        })
                        this.setData({
                            markers: this.data.markers,
                        })
                    }
                })
            }

            // 处理确定病例区域
            if (res.data.data.xiaoqu != '') {
                res.data.data.xiaoqu.forEach((v, k) => {
                    v.quClick = false;
                })
            }


            this.setData({
                infoData: res.data.data,
                realTimeData: res.data.data.xinzeng ? res.data.data.xinzeng : '',
                // latitude:
                marketData,
                guijiData: res.data.data.guiji ? res.data.data.guiji : '',
                updateTime: util.timestamp(res.data.data.update_at, 'Y-M-D h')
            })

            if (this.data.realTimeData.length > 2) {
                this.setData({
                    showReal: true,
                })
            }

            if (this.data.guijiData.length > 2) {
                this.setData({
                    showGuiji: true,
                })
            }


        })
    },

    // 搜索区域
    searchData(value) {
        let searchRes = [];
        // let quCont = [];
        searchRes = this.data.marketData.filter(v => {
            if (v.address.includes(value) || v.qu.includes(value)) {
                return v;
            }
        })
        this.setData({
            searchRes,
        })

        if (searchRes != '') {
            searchRes.forEach((v, k) => {
                if (v.location) {
                    v.location.lng = util.bMapTransQQMap(v.location.lng, v.location.lat).lng;
                    v.location.lat = util.bMapTransQQMap(v.location.lng, v.location.lat).lat;
                    this.data.resMarkers.push({
                        id: k,
                        longitude: v.location.lng,
                        latitude: v.location.lat,
                        iconPath: '/images/dwl.png',
                        label: {
                            content: v.address,
                            bgColor: "#fff",
                            fontSize: '20rpx',
                            padding: "10rpx",
                            borderColor: "#232E7D",
                            borderWidth: "1rpx",
                            borderRadius: "10rpx",
                            textAlign: 'center',
                            color: "#ff0000"
                        }
                    })
                    this.setData({
                        resMarkers: this.data.resMarkers,
                    })
                }
            })

            let tempArr = [];
            let quCont = [];
            for (let i = 0; i < searchRes.length; i++) {
                if (tempArr.indexOf(searchRes[i].qu) === -1) {
                    quCont.push({
                        qu: searchRes[i].qu,
                        xq: [searchRes[i]],
                        quClick: true,
                    });
                    tempArr.push(searchRes[i].qu);
                } else {
                    for (let j = 0; j < quCont.length; j++) {
                        if (quCont[j].qu == searchRes[i].qu) {
                            quCont[j].xq.push(searchRes[i]);
                            break;
                        }
                    }
                }
            }
            this.setData({
                quCont
            })

        }

    },

    // 省份选择
    bindProv: function(e) {
        this.setData({
            pindex: e.detail.value,
            cindex: 0,
            qindex: 0,
            quPick: ["全市"]
                // cityPick: ["选择城市"],
        })
        let clist = [];
        this.data.cityArray[this.data.pindex].citylist.forEach(v => {
            clist.push(v.cityname)
        })
        this.setData({
            // cityPick: this.data.cityPick.concat(clist),
            cityPick: clist,
        })
        let cityname = this.data.cityPick[this.data.cindex];
        if (this.data.citylist) {
            this.data.citylist.forEach(v => {
                    if (v.cityname == cityname) {
                        let citycode = v.citycode;
                        this.setData({
                            citycode,
                            city: cityname
                        })
                    }
                })
                // 获取城市公众号二维码
            this.getQrcode(this.data.citycode);
            this.getInfoData(this.data.citycode);
        }

    },

    // 城市选择
    bindCity(e) {
        this.setData({
            cindex: e.detail.value
        })
        let cityname = this.data.cityPick[this.data.cindex];
        if (this.data.citylist) {
            this.data.citylist.forEach(v => {
                if (v.cityname == cityname) {
                    let citycode = v.citycode;
                    this.setData({
                        citycode,
                        city: cityname
                    })
                }
            })
        }
        // 获取城市公众号二维码
        this.getQrcode(this.data.citycode);
        this.getInfoData(this.data.citycode);
    },

    // 区域选择
    bindQu(e) {
        this.setData({
            qindex: e.detail.value,
            showNomal: false,
        })
        let quname = this.data.quPick[this.data.qindex];
        if (quname != "全市") {
            this.searchData(quname);
        } else {
            this.setData({
                showNomal: true
            })
        }
    },

    // 进入客服会话
    contact() {
        this.setData({
            showtk: false,
        })
    },

    // 关闭
    close() {
        this.setData({
            showtk: false,
        })
    },

    // 搜索输入事件
    inputQu(e) {
        let quValue = e.detail.value;
        this.setData({
            quValue,
        })

        if (this.data.quValue == "") {
            this.setData({
                showNomal: true,
            })
        }

    },
    // 搜索区域
    searchQu(e) {
        let quValue = e.detail.value;
        this.setData({
            quValue,
            showNomal: false,
        })
        if (this.data.quValue != "") {
            this.searchData(quValue)
        } else {
            this.setData({
                showNomal: true,
            })
        }

    },

    // 查询按钮
    searchBtn() {
        this.setData({
            showNomal: false,
        })
        if (this.data.quValue != "") {
            this.searchData(this.data.quValue)
        } else {
            this.setData({
                showNomal: true,
            })
        }
        // this.searchData(this.data.quValue)
    },

    // 全市内容展示点击
    bindClick(e) {
        let flag = e.currentTarget.dataset.flag;
        if (flag == 1) {
            this.setData({
                showTitle1: true,
                showTitle2: false,
            })
        } else if (flag == 2) {
            this.setData({
                showTitle1: false,
                showTitle2: true,
            })
        }
    },

    // 实时新增跳转、病例轨迹跳转、门诊跳转、确诊区域跳转
    navDeatil(e) {
        let id = e.currentTarget.dataset.id;
        let citycode = e.currentTarget.dataset.citycode;
        let url = e.currentTarget.dataset.url;
        let type = e.currentTarget.dataset.type;
        let title = e.currentTarget.dataset.title;
        if (id != 0) {
            wx.navigateTo({
                url: '/pages/article/detail/detail?id=' + id + '&type=' + type + '&city=' + citycode,
            })
        } else {
            this.setData({
                showtk: true
            })
            util.navContact(title, url)
        }
    },

    // 确诊病例区域点击
    quClickBtn(e) {
        let quname = e.currentTarget.dataset.quname;
        let flag = e.currentTarget.dataset.flag;
        if (flag == 1) {
            this.data.infoData.xiaoqu.forEach((v, k) => {
                if (v.quname == quname) {
                    v.quClick = !v.quClick;
                }
            })
            this.setData({
                infoData: this.data.infoData,
            })
        } else {
            this.data.quCont.forEach((v, k) => {
                if (v.qu == quname) {
                    v.quClick = !v.quClick;
                }
            })
            this.setData({
                quCont: this.data.quCont,
            })
        }

    },

    // 查看更多
    seemoreBtn(e) {
        let flag = e.currentTarget.dataset.flag;
        if (flag == 1) {
            this.setData({
                showReal: false
            })
        } else if (flag == 2) {
            this.setData({
                showGuiji: false
            })
        }
    },

    /**
     * 页面滑动事件的处理函数
     */
    onPageScroll: function(e) {
        this.setData({
            showNav: false,
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        return {
            title: this.data.infoData ? this.data.infoData.title : "新冠肺炎患者分布小区查询",
        }
    },

    // 分享朋友圈
    onShareTimeline: function() {
        return {
            title: this.data.infoData ? this.data.infoData.title : "新冠肺炎患者分布小区查询",
            imageUrl: "http://imgbdb3.bendibao.com/xcx/20206/19/2020619164723_78565.jpg",
            query: { city: this.data.citycode },
        }
    },
})